package com.xkdx.guangguang.fragment.brand.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.BrandActivity;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.shareclass.BrandDetail;

public class SearchBrandFragment extends BaseFragment implements OnClickListener {
	View view;
	Button search;
	ImageView back;
	ImageView shop, entertainment, life, food;
	List<BrandDetail> list = new ArrayList<BrandDetail>();
	EditText et;
	int PageIndex = 1;
	boolean tag1 = false, tag2 = false, tag3 = false, tag4 = false;
	String BrandTag = "", BrandTag1, BrandTag2, BrandTag3, BrandTag4;
	boolean all = false;
	SearchBrandAction searchBrandAction;
	SearchBrandModule searchBrandModule;
	SearchBrandPresistence searchBrandPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.search_brand, container, false);
		findView();
		setOnClick();
		return view;
	}

	@Override
	public void findView() {
		tag1 = false;
		tag2 = false;
		tag3 = false;
		tag4 = false;

		et = (EditText) view.findViewById(R.id.search_tv);
		shop = (ImageView) view.findViewById(R.id.search_brand1);
		entertainment = (ImageView) view.findViewById(R.id.search_brand2);
		life = (ImageView) view.findViewById(R.id.search_brand3);
		food = (ImageView) view.findViewById(R.id.search_brand4);

		back = (ImageView) view.findViewById(R.id.search_back);
		search = (Button) view.findViewById(R.id.search_brand);

	}

	@Override
	public void setOnClick() {
		shop.setOnClickListener(this);
		entertainment.setOnClickListener(this);
		life.setOnClickListener(this);
		food.setOnClickListener(this);
		search.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	public void showLoading() {
		searchBrandAction = new SearchBrandAction(et.getText().toString(), BrandTag);
		searchBrandModule = new SearchBrandModule();
		searchBrandPresistence = new SearchBrandPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(searchBrandPresistence);
		searchBrandPresistence.setActitons(searchBrandAction);
		searchBrandPresistence.setModule(searchBrandModule);
		searchBrandPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (searchBrandPresistence != null) {
					searchBrandPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
		dataLoadDialog.show();

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			((BrandActivity) getActivity()).setSearchBrandList(searchBrandModule.list);
			toIntentSearchResultFragment();
		} else {
			showErrorTip(map);
		}

	}

	private void toIntentSearchResultFragment() {
		SearchBrandResultFragment searchBrandResultFragment = new SearchBrandResultFragment();
		getFragmentManager().beginTransaction().replace(R.id.fragment, searchBrandResultFragment).addToBackStack(null)
				.commit();

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search_brand1:
			tag1 = !tag1;
			if (all) {
				BrandTag = "";
			} else {
				if (tag1) {
					shop.setImageResource(R.drawable.shop_select);
				} else {
					shop.setImageResource(R.drawable.shop_unselect);
				}
				setBrandTag(tag1, tag2, tag3, tag4);

			}
			break;
		case R.id.search_brand2:
			tag2 = !tag2;
			if (all) {
				BrandTag = "";
			} else {
				if (tag2) {
					entertainment.setImageResource(R.drawable.entertainment_select);
				} else {
					entertainment.setImageResource(R.drawable.entertainment_unselect);
				}
				setBrandTag(tag1, tag2, tag3, tag4);

			}
			break;
		case R.id.search_brand3:
			tag3 = !tag3;
			if (all) {
				BrandTag = "";
			} else {
				if (tag3) {
					life.setImageResource(R.drawable.lift_select);
				} else {
					life.setImageResource(R.drawable.lift_unselect);
				}
				setBrandTag(tag1, tag2, tag3, tag4);
			}
			break;
		case R.id.search_brand4:
			tag4 = !tag4;
			if (all) {
				BrandTag = "";
			} else {
				if (tag4) {
					food.setImageResource(R.drawable.food_select);
				} else {
					food.setImageResource(R.drawable.food_unselect);
				}
				setBrandTag(tag1, tag2, tag3, tag4);
			}
			break;
		case R.id.search_brand:
			showLoading();
			break;
		case R.id.search_back:
			getFragmentManager().popBackStack();
			break;
		default:
			break;
		}

	}

	// 设置BrandTag
	private void setBrandTag(boolean tag1, boolean tag2, boolean tag3, boolean tag4) {
		BrandTag = "";
		List<String> list = new ArrayList<String>();
		if (tag1) {
			list.add("1");
		}
		if (tag2) {
			list.add("2");
		}
		if (tag3) {
			list.add("3");
		}
		if (tag4) {
			list.add("4");
		}
		for (int i = 0; i < list.size(); i++) {

			if (i == 0) {
				BrandTag += list.get(i);
			} else {
				BrandTag += "|" + list.get(i);
			}

		}
	}

}
